package com.arm.controller.product;

import com.arm.controller.WechatBaseController;
import com.arm.def.Constant;
import com.arm.module.ProductEntity;
import com.arm.service.ProductEntityService;
import com.arm.tools.result.ResultObj;
import com.arm.tools.result.ReturnCode;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Baggio on 2019-1-8.
 */
@RestController
@RequestMapping("productEntity")
public class ProductEntityController extends WechatBaseController {
    @Autowired
    ProductEntityService productEntityService;


    /**
     * 产品列表
     *
     * @return
     */
    @RequestMapping(value = "/productEntityList")
    public ModelAndView productEntityList() {
        ModelAndView mov = new ModelAndView();
        mov.setViewName("weChat/productEntity/productEntityList");
        return mov;
    }

    /**
     * 产品页面
     *
     * @param productEntityId
     * @return
     */
    @RequestMapping(value = "{productEntityId}")
    public ModelAndView productEntityList(@PathVariable("productEntityId") Long productEntityId) {
        ModelAndView mov = new ModelAndView();
        if (super.getIsMobile(super.httpServletRequest)) {
            super.oauth2(super.httpServletRequest, super.httpServletResponse);
        }

        ProductEntity productEntity = productEntityService.getProductEntityByProductEntityId(productEntityId);

        mov.addObject("productEntityId", productEntityId);
        mov.addObject("productEntity", productEntity);
        mov.setViewName("weChat/productEntity/productEntity");
        return mov;
    }


    @GetMapping(value = "/list")
    public ResultObj list(Integer page, Integer pageSize) {
        ResultObj resultObj = new ResultObj();
        try {

            if (page == null || page == 0) {
                page = Constant.PAGENUM;
            }
            // 设置每页记录数
            if (pageSize == null || pageSize == 0) {
                pageSize = Constant.PAGESIZENum;
            }
            String pageStr = String.valueOf(page);
            String pageSizeStr = String.valueOf(pageSize);
            List<ProductEntity> productEntityList = productEntityService.getProductEntityList(new HashMap<String, String>() {
                {
                    put("page", pageStr);
                    put("pageSize", pageSizeStr);
                }
            });
            PageInfo<ProductEntity> pageinfo = new PageInfo<>(productEntityList);
            resultObj.setData(pageinfo);

        } catch (Exception e) {
            e.printStackTrace();
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(e.getMessage());
        }
        return resultObj;
    }
}
